<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="sysUserId">
        <el-input
          v-model="queryParams.sysUserId"
          placeholder="编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择角色" prop="userRole">
        <el-select v-model="queryParams.userRole" placeholder="选择角色" clearable>
          <el-option v-for="dict in dict.type.user_role" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:common:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:common:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:common:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:common:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <!--  -->
    <el-table v-loading="loading" :data="commonList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="编号" align="left" prop="sysUserId" />
      <el-table-column label="店铺/代理商名称" align="left" prop="nickName" />
      <el-table-column label="角色" align="left" prop="userRole">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_role" :value="scope.row.userRole" />
        </template>
      </el-table-column>
      <el-table-column label="总收益" align="left" prop="totalAmount">
      <template slot-scope="scope">
          <span style="font-weight: bold;color: #f00;">¥{{scope.row.totalAmount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="待结算金额" align="left" prop="unsettledAmount">
      <template slot-scope="scope">
          <span style="font-weight: bold;color: #f00;">¥{{scope.row.unsettledAmount}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="可提现金额" align="left" prop="balanceAmount">-->
<!--      <template slot-scope="scope">-->
<!--          <span style="font-weight: bold;color: #f00;">¥{{scope.row.balanceAmount}}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="已提现金额" align="left" prop="withdrawAmount">
      <template slot-scope="scope">
          <span style="font-weight: bold;color: #f00;">¥{{scope.row.withdrawAmount}}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商代参数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="省代理" prop="roleProvince">
          <el-input v-model="form.roleProvince" placeholder="请输入省代理" />
        </el-form-item>
        <el-form-item label="分成比例" prop="provinceRatio">
          <el-input v-model="form.provinceRatio" placeholder="请输入分成比例" />
        </el-form-item>
        <el-form-item label="市代理" prop="roleCity">
          <el-input v-model="form.roleCity" placeholder="请输入市代理" />
        </el-form-item>
        <el-form-item label="分成比例" prop="cityRatio">
          <el-input v-model="form.cityRatio" placeholder="请输入分成比例" />
        </el-form-item>
        <el-form-item label="县代理" prop="roleCount">
          <el-input v-model="form.roleCount" placeholder="请输入县代理" />
        </el-form-item>
        <el-form-item label="分成比例" prop="countRatio">
          <el-input v-model="form.countRatio" placeholder="请输入分成比例" />
        </el-form-item>
        <el-form-item label="平台" prop="roleService">
          <el-input v-model="form.roleService" placeholder="请输入平台" />
        </el-form-item>
        <el-form-item label="分成比例" prop="serviceRatio">
          <el-input v-model="form.serviceRatio" placeholder="请输入分成比例" />
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker clearable
            v-model="form.beginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人" prop="editUser">
          <el-input v-model="form.editUser" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="逻辑删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="创建时间" prop="crTime">
          <el-date-picker clearable
            v-model="form.crTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="upTime">
          <el-date-picker clearable
            v-model="form.upTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备用字段1" prop="commonField1">
          <el-input v-model="form.commonField1" placeholder="请输入备用字段1" />
        </el-form-item>
        <el-form-item label="备用字段2" prop="commonField2">
          <el-input v-model="form.commonField2" placeholder="请输入备用字段2" />
        </el-form-item>
        <el-form-item label="备用字段3" prop="commonField3">
          <el-input v-model="form.commonField3" placeholder="请输入备用字段3" />
        </el-form-item>
        <el-form-item label="备用字段4" prop="commonField4">
          <el-input v-model="form.commonField4" placeholder="请输入备用字段4" />
        </el-form-item>
        <el-form-item label="备用字段5" prop="commonField5">
          <el-input v-model="form.commonField5" placeholder="请输入备用字段5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWallet, getWallet, delWallet, addWallet, updateWallet } from "@/api/system/wallet";

export default {
  name: "Common",
  dicts: ['user_role','common_role'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商代参数表格数据
      commonList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysUserId: null,
        nickName: null,
        userRole: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商代参数列表 */
    getList() {
      this.loading = true;
      listWallet(this.queryParams).then(response => {
        this.commonList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commonId: null,
        accountType: null,
        roleProvince: null,
        provinceRatio: null,
        roleCity: null,
        cityRatio: null,
        roleCount: null,
        countRatio: null,
        roleService: null,
        serviceRatio: null,
        commonStatus: null,
        beginTime: null,
        endTime: null,
        editUser: null,
        delFlag: null,
        crTime: null,
        upTime: null,
        commonField1: null,
        commonField2: null,
        commonField3: null,
        commonField4: null,
        commonField5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commonId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商代参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const commonId = row.commonId || this.ids
      getCommon(commonId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商代参数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.commonId != null) {
            updateCommon(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommon(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commonIds = row.commonId || this.ids;
      this.$modal.confirm('是否确认删除商代参数编号为"' + commonIds + '"的数据项？').then(function() {
        return delCommon(commonIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/common/export', {
        ...this.queryParams
      }, `common_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
